package com.yuyou.fn.timer.task;

import cn.beststudy.member.*;
import com.github.wens.mybatisplus.examples.Example;
import com.google.common.collect.Lists;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.log.XxlJobLogger;
import com.yuyou.fn.common.service.ISiteConfigService;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Urls;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.BaseGrade;
import com.yuyou.fn.educational.entity.CustomerBindStudent;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.service.IBaseGradeService;
import com.yuyou.fn.educational.service.ICustomerBindStudentService;
import com.yuyou.fn.educational.service.ICustomerUserService;
import com.yuyou.fn.educational.service.IStudentService;
import com.yuyou.fn.educational.vo.StudentSaveOrUpdateVo;
import com.yuyou.fn.platform.entity.CustomerUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 根据.cid 更新学生姓名手机号
 */
@JobHander(value = "UpdateStudentByMemberTask")
@Component
public class UpdateStudentByMemberTask extends IJobHandler {

    @Resource
    private IStudentService studentService ;


    @Resource
    private ISiteConfigService siteConfigService ;

    private RegistrationService registrationService ;




    @Override
    public ReturnT<String> execute(String... params) throws Exception {

        registrationService =  new RegistrationService(Urls.create(siteConfigService.getConfigValueFromCache("member_webservice_url")));

        try {
            Date date= TimeUtils.now();
            date=TimeUtils.changeMinute(date,-4);
            PageResultDTO memberInfoUpdate = registrationService.getRegistrationServiceSoap().getSyncMemberInfoUpdate(Utils.formatDate(date, "yyyy-MM-dd HH:mm:ss"), 1, 20000);
            ArrayOfMemberProfileSimple memberInfoUpdateData = memberInfoUpdate.getData();
            List<MemberProfileSimple> memberProfileSimples = memberInfoUpdateData.getMemberProfileSimple();
            memberProfileSimples.stream().forEach(memberProfileSimple -> {
                try {
                    studentService.updateNameAndPhoneAndSlavePhoneByCid(memberProfileSimple.getMemberID(),memberProfileSimple.getFullName(),memberProfileSimple.getMainPhoneNum(),memberProfileSimple.getSlavePhoneNum());
                }catch (Exception e){
                    XxlJobLogger.log("cid={0},ex={1}",memberProfileSimple.getMemberID(),e);
                }
            });


        } catch (Exception e) {
            XxlJobLogger.log("同步学生姓名手机数据失败:{0}",e);
            return ReturnT.FAIL;
        }

        return ReturnT.SUCCESS;
    }

    private void bindToMemberCenter(String memberId, Long studentId) {
        Student student = studentService.findById(studentId , "cid");

        if(StringUtils.isNotEmpty(student.getCid())){
            try{
                registrationService.getRegistrationServiceSoap().changeMem2MemRelationIsValid(memberId,student.getCid(),true );
            }catch (Exception e){
                e.printStackTrace();
                //
            }
        }
    }

}
